package com.cheng.web.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cheng.bos.actionUtils.ActionUtils;
import com.cheng.domain.system.Menu;
import com.cheng.domain.system.User;
import com.cheng.service.system.MenuService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.ActionContext;

@Namespace("/menu")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class MenuAction extends ActionUtils<Menu>{
	
	@Autowired
	private MenuService menuService;

	@Action(value="menu_query",results={@Result(name="success",type="json")})
	public String findMenu(){
		List<Menu> list = menuService.findMenu();
		ActionContext.getContext().getValueStack().push(list);
		return  SUCCESS;
	}
	@Action(value="menu_save",results={@Result(name="success",type="redirect",location="../pages/system/menu.html")})
	public String saveMenu(){
		menuService.saveMenu(model);
		return  SUCCESS;
	}
	
	@Action(value="menu_list")
	public String findAllMenu() throws Exception{
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		List<Menu> list = menuService.findMenuByUser(user);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(list);
		ServletActionContext.getResponse().setContentType("application/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().write(json);
		return NONE;
	}
	
}
